<?
#####################################################
#  COPYRIGHT NOTICE
#####################################################
# In the following statements, the term "this 
# program" refers to all files associated with the
# execution and distribution of Adtrackz.
#
# This Program is Copyright 2004 Jonah Klimack.
# All Rights Reserved. 
#
# Selling the code for this program, modifying 
# and/or redistributing the code for this program
# in part or in whole over the Internet or in any 
# other medium is expressly forbidden. Violators
# will be prosecuted to the fullest extent of the law
# Copyright and header information may not be
# modified.
#
# This program is distributed "as is" and without 
# warranty of any kind, either express or implied.
# In no event shall the liability of Jonah Klimack 
# for any damages, losses and/or causes of 
# action exceed the total amount paid by the 
# user for this software.		
#####################################################
# DO NOT MODIFY ANYTHING IN THIS FILE
#####################################################


require "header.php";	

?>
<p class="small" align="left">
Split Test > Statistics
</p>
<p class="heading">
Your Split Test Statistics
</p>
<table width="100%">
<tr>
	<td>Select a split test to display.</td>
	<td align="right"><a href="preferences.php">Change Preferences...</td>
</tr>
</table>
<br>

<form action="st_stats.php" method="post">
<!--MAIN CONTROLS-->
<table width="350" border="0" cellpadding="0" cellspacing="0" align="center" valign="center">
<tr>
	<td>Split Test</td>
	<td>Time Period</td>
</tr>
<tr>
	<td>
		<select name=split_test class=small>
		<?
			$split_tests = mysql_query("select * from adtrackz_split_test_groups order by name asc");
			while($st = mysql_fetch_row($split_tests))
			{
				if ($split_test == $st[0])
					echo "<option value=$st[0] selected>$st[1]</option>";
				else
					echo "<option value=$st[0]>$st[1]</option>";
			}
		?>
		</select>
	</td>
	<td>
		<?
			if ($timeperiod == "all")
				$tp_all_selected = "selected";
			elseif ($timeperiod == "today")
				$tp_today_selected = "selected";
			elseif ($timeperiod == "yesterday")
				$tp_yesterday_selected = "selected";
			elseif ($timeperiod == "thisweek")
				$tp_thisweek_selected = "selected";
			elseif ($timeperiod == "lastweek")
				$tp_lastweek_selected = "selected";
			elseif ($timeperiod == "thismonth")
				$tp_thismonth_selected = "selected";
			elseif ($timeperiod == "lastmonth")
				$tp_lastmonth_selected = "selected";
			elseif ($timeperiod == "thisyear")
				$tp_thisyear_selected = "selected";
			elseif ($timeperiod == "lastyear")
				$tp_lastyear_selected = "selected";
		?>
		<select name="timeperiod" class="small">
		<option value="all" <?=$tp_all_selected?>>All Clicks</option>
		<option value="today" <?=$tp_today_selected?>>Today</option>
		<option value="yesterday" <?=$tp_yesterday_selected?>>Yesterday</option>
		<option value="thisweek" <?=$tp_thisweek_selected?>>This Week</option>
		<option value="lastweek" <?=$tp_lastweek_selected?>>Last Week</option>
		<option value="thismonth" <?=$tp_thismonth_selected?>>This Month</option>
		<option value="lastmonth" <?=$tp_lastmonth_selected?>>Last Month</option>
		<option value="thisyear" <?=$tp_thisyear_selected?>>This Year</option>
		<option value="lastyear" <?=$tp_lastyear_selected?>>Last Year</option>
		</select>
	</td>
	<td>
		<INPUT TYPE="submit" name="submit_top" value="Build Report -->" height="12" width="15" class="small">
	</td>
</tr>
</table>
<br>
<br>
<?
//CODE FOR MAIN STATS TABLE

//get teh campaigns for each split test group
$campaigns = mysql_query("select campaignID from adtrackz_split_test_campaigns where split_test_id='$split_test'");

while ($campaign = mysql_fetch_row($campaigns))
{
	$adcode = mysql_fetch_row(mysql_query("select adcode from adtrackz_campaigns where ID='$campaign[0]'"));
	echo mysql_error();
	$campaignIDs[$campaign[0]] = $adcode[0];	
}

//sort array
if ($campaignIDs)
	asort($campaignIDs);

//now we have our list of campaigns to display in the stats report
if ($campaignIDs)
{

	if ($submit_top)
	{

		//CONVERT TIME PERIOD INTO TIME STAMP
		if ($timeperiod == "all")
		{
			$from_timestamp = mktime (0,0,0,1,1,2004); 
			$to_timestamp = mktime (0,0,0,12,31,2014);  
		}
		elseif ($timeperiod == "today")
		{
			$from_timestamp = mktime (0,0,0,date("m"),date("d"),date("Y")); 
			$to_timestamp = mktime (23,59,59,date("m"),date("d"),date("Y")); 
		}
		elseif ($timeperiod == "yesterday")
		{
			$from_timestamp = mktime (0,0,0,date("m"),date("d")-1,date("Y")); 
			$to_timestamp = mktime (23,59,59,date("m"),date("d")-1,date("Y")); 
		}
		elseif ($timeperiod == "thisweek")
		{
		//0 (for Sunday) through 6 (for Saturday)
			$day_of_week = date("w");
			$from_timestamp = mktime (0,0,0,date("m"),date("d")-$day_of_week,date("Y")); 
			$to_timestamp = mktime (23,59,59,date("m"),date("d")+(6-$day_of_week),date("Y")); 
		}
		elseif ($timeperiod == "lastweek")
		{
			$day_of_week = date("w");
			$from_timestamp = mktime (0,0,0,date("m"),date("d")-(7+$day_of_week),date("Y")); 
			$to_timestamp = mktime (23,59,59,date("m"),date("d")-($day_of_week+1),date("Y")); 
		}
		elseif ($timeperiod == "thismonth")
		{
			$days_in_month = date("t");
			$from_timestamp = mktime (0,0,0,date("m"),01,date("Y")); 
			$to_timestamp = mktime (23,59,59,date("m"),$days_in_month,date("Y")); 
		}
		elseif ($timeperiod == "lastmonth")
		{
			$days_in_month = date("t");
			$from_timestamp = mktime (0,0,0,date("m")-1,01,date("Y")); 
			$to_timestamp = mktime (23,59,59,date("m")-1,$days_in_month,date("Y"));
		}
		elseif ($timeperiod == "thisyear")
		{
			$from_timestamp = mktime (0,0,0,01,01,date("Y")); 
			$to_timestamp = mktime (23,59,59,12,31,date("Y"));
		}
		elseif ($timeperiod == "lastyear")
		{
			$from_timestamp = mktime (0,0,0,01,01,date("Y")-1); 
			$to_timestamp = mktime (23,59,59,12,31,date("Y")-1);
		}
		else
			alert("invalid time period");	
	}
	elseif ($submit_bottom)
	{
		$from_timestamp = mktime (0,0,0,$fmonth,$fday,$fyear); 
		$to_timestamp = mktime (23,59,59,$tmonth,$tday,$tyear); 
	}

	if ($from_timestamp > $to_timestamp)
	{
		alert("The from time period you have chosen should be earlier than the to time period.");
		echo "<br>";
	}


	//fetch preferences data
	$pref = mysql_fetch_assoc(mysql_query("select * from adtrackz_preferences"));

	//THE MAIN STATS TABLE
	echo "<!--- THE MAIN STATS TABLE -->";
	echo "<table border=1 cellspacing=0 cellpadding=5 align=center>";
	echo "<tr bgcolor=#FA7F14 >";
	echo "<td class=tbhead2>Campaigns</td>";
	if ($pref['rawclicks'])
		echo "<td class=tbhead2>Raw Clicks</td>";
	if ($pref['clicks'])
		echo "<td class=tbhead2>Clicks</td>";
	if ($pref['actions'])
		echo "<td class=tbhead2>Actions</td>";
	if ($pref['sales'])
		echo "<td class=tbhead2>Sales</td>";
	if ($pref['cta'])
		echo "<td class=tbhead2>CTA</td>";
	if ($pref['cts'])
		echo "<td class=tbhead2>CTS</td>";
	if ($pref['cpc'])
		echo "<td class=tbhead2>Avg CPC</td>";
	if ($pref['cpa'])
		echo "<td class=tbhead2>CPA</td>";
	if ($pref['cps'])
		echo "<td class=tbhead2>CPS</td>";
	if ($pref['cost'])
		echo "<td class=tbhead2>Cost</td>";
	if ($pref['revenue'])
		echo "<td class=tbhead2>Revenue</td>";
	if ($pref['profit'])
		echo "<td class=tbhead2>Profit</td>";
	if ($pref['roi'])
		echo "<td class=tbhead2>R.O.I.</td>";
	echo "</tr>";


	//MAIN STATS FOREACH LOOP - loop through each campaign
	 $c = 0; //to keep track of alternating rows for bg color
	foreach ($campaignIDs as $campaignID => $adcode1)
	{
		//fetch campaign data for later use
		$campaign = mysql_fetch_assoc(mysql_query("select * from adtrackz_campaigns where ID='$campaignID'"));

		//affiliate tracking is the same as purpose=all for stats table
		if ($campaign['purpose'] == "affiliate")
			$campaign['purpose'] = "all";

		$rawclickdata = mysql_fetch_row(mysql_query("select count(ID) from adtrackz_clicks where campaignID='$campaignID' && subID = 0 && timest >= '$from_timestamp' && timest <= '$to_timestamp'")); 
		$rawclicks = $rawclickdata[0];

		$clickdata = mysql_fetch_row(mysql_query("select count(ID) from adtrackz_clicks where campaignID='$campaignID' && subID = 0 && timest >= '$from_timestamp' && timest <= '$to_timestamp' && uniqueflag=1"));
		$clicks = $clickdata[0];

		//CAMPAIGN ACTIONS
		$actiondata = mysql_fetch_row(mysql_query("select count(ID) from adtrackz_actions where campaignID='$campaignID' && subID = 0 && timest >= '$from_timestamp' && timest <= '$to_timestamp'"));
		$actions = $actiondata[0];

		//CAMPAIGN SALES
		$salesdata = mysql_fetch_row(mysql_query("select count(ID) from adtrackz_sales where campaignID='$campaignID' && subID = 0 && timest >= '$from_timestamp' && timest <= '$to_timestamp'"));
		$sales = $salesdata[0];

		//CALCULATE COST
		if ($campaign['ppc'])
		{
			//true cost
			$cpc_data = mysql_fetch_row(mysql_query("select sum(cpc) from adtrackz_clicks where campaignID='$campaignID' && subID = 0 && timest >= '$from_timestamp' && timest <= '$to_timestamp' && uniqueflag = 1"));
			//cost is sum of all clicks in this period, 
			//remember that in go.php if click doesn't have cpc set, then default cpc from campaign is used
			//therefore, cpc should always have a value
			$cost = $cpc_data[0];
		}
		elseif ($campaign['recurdays'] && $campaign['repeat']) 
		{
			//cost = from start date to now 
			$now = time();
			$startdate = strtotime($campaign['startdate']);

			//cycles so far, expressed in days
			$cycles = (($now - $startdate) / 86400) / $campaign['recurdays']; 
			if ($cycles > $campaign['repeat']) //user defines how many cycles
				$cycles = $campaign['repeat'];
			$cost = $cycles * $campaign['cost']; //abs cost is # of cycles * specified campaign cost				
		}
		else //normal cost
			$cost = $campaign['cost'];

		//CALCULATE VALUES - get rid of zeros
		$cta = (!$clicks ? 0 : $actions / $clicks * 100);	
		$cts = (!$clicks ? 0 : $sales / $clicks * 100);
		$cpc = (!$clicks ? 0 : $cost / $clicks);
		$cpa = (!$actions ? 0 : $cost / $actions);
		$cps = (!$sales ? 0 : $cost / $sales);
		$revdata = mysql_fetch_row(mysql_query("select sum(revenue) from adtrackz_sales where campaignID='$campaignID' && subID = 0 && timest >= '$from_timestamp' && timest <= '$to_timestamp'"));
		$rev = $revdata[0];
		$profit = $rev - $cost;
		//$roi = $revenue / $cost * 100	??
		$roi = (!$cost ? 0 : ($profit < 0 ? 0 : ($profit / $cost) * 100));

		//FORMAT VALUES
		$rawclicks = number_format($rawclicks, 0);
		$clicks = number_format($clicks, 0);
		$actions = number_format($actions, 0);
		$sales = number_format($sales, 0);
		$cta = (!$cta ? 0 : number_format($cta, 2));
		$cts = (!$cts ? 0 : number_format($cts, 2));
		$cpc = (!$cpc ? 0 : number_format($cpc, 2));
		$cpa = (!$cpa ? 0 : number_format($cpa, 2));
		$cps = (!$cps ? 0 : number_format($cps, 2));
		$rev = (!$rev ? 0 : number_format($rev, 2));
		$cost = (!$cost ? 0 : number_format($cost, 2));
		$profit = (!$profit ? 0 : number_format($profit, 2));		
		$roi = (!$roi ? 0 : number_format($roi, 2));

		//SET STATUSPIC
		$now = date("Y-m-d H:i:s"); 
		if ((earlier_date ($campaign['startdate'], $now) == $campaign['startdate']) && (earlier_date ($campaign['enddate'], $now) == $now))
			$statuspic = "inprogress3.gif";
		elseif ((earlier_date ($campaign['startdate'], $now) == $campaign['startdate']) && (earlier_date ($campaign['enddate'], $now) == $campaign['enddate']))
			$statuspic = "ended3.gif";
		elseif (earlier_date ($campaign['startdate'], $now) == $now)
			$statuspic = "scheduled3.gif";

		//alternate bg colors between campaigns
		$c++;
		//need better colors
		if (!($c % 2))
			$tr = "<tr bgcolor=#F0F0F0>";
		else
			$tr = "<tr bgcolor=white>";
		echo $tr;
		echo '<td align="left"><img src="'.$statuspic.'">	<a href="dstats-summary.php?cID='.$campaign['ID'].'">'.$campaign['adcode'].'</a></td>';
		if ($pref['rawclicks'])
			echo "<td class=tb1>$rawclicks</td>";
		if ($pref['clicks'])
			echo "<td class=tb1>$clicks</td>";
		if ($pref['actions'])
			echo "<td class=tb1>$actions</td>";
		if ($pref['sales'])
			echo "<td class=tb1>$sales</td>";
		if ($pref['cta'])
			echo "<td class=tb1>$cta%</td>";
		if ($pref['cts'])
			echo "<td class=tb1>$cts%</td>";
		if ($pref['cpc'])
			echo "<td class=tb1>\$$cpc</td>";
		if ($pref['cpa'])
			echo "<td class=tb1>\$$cpa</td>";
		if ($pref['cps'])
			echo "<td class=tb1>\$$cps</td>";
		if ($pref['cost'])
			echo "<td class=tb1>\$$cost</td>";
		if ($pref['revenue'])
			echo "<td class=tb1>\$$rev</td>";
		if ($pref['profit'])
		{
			if ($profit < 0)
				echo "<td class=tb1><font color=#F51800>\$$profit</font></td>";
			else
				echo "<td class=tb1>\$$profit</td>";
		}
		if ($pref['roi']) //but isn't roi technically negative when it's below 100%?
			echo "<td class=tb1>$roi%</td>";
		echo "</tr>";



		//LIST OF SUBCAMPAIGNS FOR THIS CAMPAIGN
		$subcampaigns = mysql_query("select * from adtrackz_subcampaigns where campaignID='$campaignID'");
		while ($sub = mysql_fetch_assoc($subcampaigns))
		{
			//SUBCAMPAIGN CLICKS
			$sub_rawclicksdata = mysql_fetch_row(mysql_query("select count(ID) from adtrackz_clicks where campaignID='$campaignID' && subID = ".$sub['ID']." && timest >= '$from_timestamp' && timest <= '$to_timestamp'"));
			$sub_rawclicks = $sub_rawclicksdata[0];

			$sub_clicksdata = mysql_fetch_row(mysql_query("select count(ID) from adtrackz_clicks where campaignID='$campaignID' && subID = ".$sub['ID']." && timest >= '$from_timestamp' && timest <= '$to_timestamp' && uniqueflag=1"));
			$sub_clicks = $sub_clicksdata[0];

			//SUBCAMPAIGN ACTIONS
			$sub_actionsdata = mysql_fetch_row(mysql_query("select count(ID) from adtrackz_actions where campaignID='$campaignID' && subID = ".$sub['ID']." && timest >= '$from_timestamp' && timest <= '$to_timestamp'"));
			$sub_actions = $sub_actionsdata[0];

			//SUBCAMPAIGN SALES
			$sub_salesdata = mysql_fetch_row(mysql_query("select count(ID) from adtrackz_sales where campaignID='$campaignID' && subID = ".$sub['ID']." && timest >= '$from_timestamp' && timest <= '$to_timestamp'"));
			$sub_sales = $sub_salesdata[0];

			//CALCULATE COST
			if ($sub['ppc'])
			{
				$sub_cpc_data = mysql_fetch_row(mysql_query("select sum(cpc) from adtrackz_clicks where campaignID='$campaignID' && subID = ".$sub['ID']." && timest >= '$from_timestamp' && timest <= '$to_timestamp' && uniqueflag = 1"));
				//cost is sum of all clicks in this period, 
				//remember that in go.php if click doesn't have cpc set, then default cpc from campaign is used
				//therefore, cpc should always have a value
				$sub_cost = $sub_cpc_data[0];
			}
			else //normal cost
			{
				$sub_cost = $sub['cost'];
			}

			//CALCULATE OTHER VALUES - get rid of zeros
			$sub_cta = (!$sub_clicks ? 0 : $sub_actions / $sub_clicks * 100);	
			$sub_cts = (!$sub_clicks ? 0 : $sub_sales / $sub_clicks * 100);
			$sub_cpc = (!$sub_clicks ? 0 : $sub_cost / $sub_clicks);		
			$sub_cpa = (!$sub_actions ? 0 : $sub_cost / $sub_actions);
			$sub_cps = (!$sub_sales ? 0 : $sub_cost / $sub_sales);
			$sub_revdata = mysql_fetch_row(mysql_query("select sum(revenue) from adtrackz_sales where campaignID='$campaignID' && subID = ".$sub['ID']." && timest >= '$from_timestamp' && timest <= '$to_timestamp'"));
			$sub_rev = $sub_revdata[0];
			$sub_profit = $sub_rev - $sub_cost;
			//$roi = $revenue / $cost * 100	??
			$sub_roi = (!$sub_cost ? 0 : ($sub_profit < 0 ? 0 : $sub_profit / $sub_cost * 100));

			//FORMAT VALUES
			$sub_rawclicks = number_format($sub_rawclicks, 0);
			$sub_clicks = number_format($sub_clicks, 0);
			$sub_actions = number_format($sub_actions, 0);
			$sub_sales = number_format($sub_sales, 0);
			$sub_cta = (!$sub_cta ? 0 : number_format($sub_cta, 2));
			$sub_cts = (!$sub_cts ? 0 : number_format($sub_cts, 2));
			$sub_cpc = (!$sub_cpc ? 0 : number_format($sub_cpc, 2));
			$sub_cpa = (!$sub_cpa ? 0 : number_format($sub_cpa, 2));
			$sub_cps = (!$sub_cps ? 0 : number_format($sub_cps, 2));
			$sub_rev = (!$sub_rev ? 0 : number_format($sub_rev, 2));
			$sub_cost = (!$sub_cost ? 0 : number_format($sub_cost, 2));
			$sub_profit = (!$sub_profit ? 0 : number_format($sub_profit, 2));		
			$sub_roi = (!$sub_roi ? 0 : number_format($sub_roi, 2));

			if (!($c % 2))
				$tr = "<tr bgcolor=#F0F0F0>";
			else
				$tr = "<tr bgcolor=white>";
			echo $tr;
			echo '<td align="right"><a href="dstats-summary.php?cID='.$campaign['ID'].'&sID='.$sub['ID'].'">'.$sub['adcode'].'</a></td>';
			if ($pref['rawclicks'])
				echo "<td class=tb1>$sub_rawclicks</td>";
			if ($pref['clicks'])
				echo "<td class=tb1>$sub_clicks</td>";
			if ($pref['actions'])
				echo "<td class=tb1>$sub_actions</td>";
			if ($pref['sales'])
				echo "<td class=tb1>$sub_sales</td>";
			if ($pref['cta'])
				echo "<td class=tb1>$sub_cta%</td>";
			if ($pref['cts'])
				echo "<td class=tb1>$sub_cts%</td>";
			if ($pref['cpc'])
				echo "<td class=tb1>\$$sub_cpc</td>";
			if ($pref['cpa'])
				echo "<td class=tb1>\$$sub_cpa</td>";
			if ($pref['cps'])
				echo "<td class=tb1>\$$sub_cps</td>";
			if ($pref['cost'])
				echo "<td class=tb1>\$$sub_cost</td>";
			if ($pref['revenue'])
				echo "<td class=tb1>\$$sub_rev</td>";
			if ($pref['profit'])
			{
				if ($sub_profit < 0)
					echo "<td class=tb1><font color=#F51800>\$$sub_profit</font></td>";
				else
					echo "<td class=tb1>\$$sub_profit</td>";
			}
			if ($pref['roi']) //but isn't roi technically negative when it's below 100%?
					echo "<td class=tb1>$sub_roi%</td>";
			echo "</tr>";
		}
	} // end campaigns loop
}
else //no campaigns from filters
	echo "<br><br><table><tr><td>There are no campaigns to display. Please try another group, status or time period.</td></tr>";

//the end
echo "</table><br><br>";

require "footer.php";
?>